﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MS.InformationManagement.Core.Documentset;
using MS.InformationManagement.DAL.DocumentCenter;
using MS.InformationManagement.Support.Constants;
using MS.InformationManagement.Core.Support;
using Microsoft.SharePoint;
using MS.InformationManagement.Core.TransactionsResponse;
using MS.InformationManagement.Support.Enums;

namespace MS.InformationManagement.Core.AssociatedActions
{
    public class ExpedienteAsociation : BaseAsociationActions
    {
        #region Constructors

        public ExpedienteAsociation(string asocitiationRadicados, DAL.DocumentCenter.Expediente currentExpediente, string targetLibrary, IRadicado radicado, Radicado radicadoEntity, string subjectExpediente)
            : base(asocitiationRadicados, targetLibrary, radicado, currentExpediente, radicadoEntity, subjectExpediente)
        { 
        
        }

        #endregion

        #region Override Methods

        public override ITransactionResponse Execute()
        {
            TomoActions.TomoActions coreTomo = new TomoActions.TomoActions();
            bool isNewExpediente = !this.getExpedienteIfExists(this.AsociationRadicados);
            var parentDocument = this.GetDocumentSetByRadicado(this.AsociationRadicados, CustomSiteColumns.FIELD_DOCUMENT_RADICADO);

            if (isNewExpediente)
                this.CreateExpediente(parentDocument);
            
            CurrentTomoEntity = coreTomo.getVolumeIfExists(CurrentExpediente.Folder.Name);
            this.updateNewRadicado(parentDocument, isNewExpediente);

            string newId = this.RadicadoBussinesObject.NewRadicado(CurrentExpediente.Folder);

            this.updateNewRadicado(this.RadicadoBussinesObject.CurrentDocumentSet, false);

            //Mover radicado a expediente
            if (isNewExpediente)
            {
                SPContentTypeId currentContentType = parentDocument.ParentList.ContentTypes[parentDocument.ContentType.Name].Id;
                parentDocument.Folder.MoveTo(CurrentExpediente.ParentList.RootFolder.Url + "/" + CurrentExpediente.Folder.Name + "/" + parentDocument.Folder.Name);
                this.UpdateContentType(currentContentType, CurrentExpediente.ParentList.ID, parentDocument.Folder.Item.ID);
            }
            else
            {
                // Se actualiza la fecha final para la pantalla de busqueda de expedientes
                this.CurrentExpediente.Folder.Item[CustomSiteColumns.FIELD_EXPEDIENTE_END] = DateTime.Now;
                this.CurrentExpediente.Folder.Item.Update();
                this.CurrentExpediente.Folder.Update();
            }

            coreTomo.UpdateDateInVolume((int)CurrentTomoEntity.Id);

            //Result
            ITransactionResponse response = new BaseRadicadoOperationResponse();
            response.Result = true;
            response.ResponseMessage = string.Format(GlobalConstants.SUCCESS_RADICADO_ASSOCIATION, newId, CurrentExpediente.Folder.Name);

            this.RadicadoBussinesObject.CurrentDocumentSet.Log(string.Empty, DateTime.Now, TraceableOperation.Radicado, SPContext.Current.Web.CurrentUser.LoginName, response.ResponseMessage);
            this.RadicadoBussinesObject.CurrentDocumentSet.Folder.Item.Update();
            this.RadicadoBussinesObject.CurrentDocumentSet.Folder.Update();

            return response;
        }

        #endregion
    }
}
